import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../app_files.dart';
import '../../../PublicSource/widgets/fileimage_ex.dart';
import 'action.dart';
import 'state.dart';

Widget buildView(
    UserInfoState state, Dispatch dispatch, ViewService viewService) {
  List<String> _titles = [
    '头像',
    '姓名',
    '联系方式',
    '部门',
    '当前角色',
  ];
  UserInfoEntity info = state.userInfo;
  List<String> _values = [
    info.headimg,
    info.nickname,
    info.phone,
    info.department,
    info.currentRoleName,
  ];

  List<Widget> _items() {
    List<Widget> list = [];
    for (int i = 0; i < _titles.length; ++i) {
      list.add(
        Container(
          height: 50,
          child: InkWell(
            child: Row(
              children: [
                Text(
                  _titles[i],
                  style: TextThemeConfig.Font14WithColor22,
                ),
                Expanded(child: Container()),
                i == 0
                    ? Row(
                        children: [
                          ClipRRect(
                            borderRadius:
                                BorderRadius.all(Radius.circular(16.5)),
                            child: (state.selectImage == null &&
                                    state.userInfo.headimg.isEmpty)
                                ? CustomImage(
                                    state.userInfo.headimg,
                                    width: 33,
                                    height: 33,
                                    fit: BoxFit.fill,
                                    placeholderUrl: 'images/default_head.png',
                                  )
                                : FadeInImage(
                                    placeholder:
                                        AssetImage("images/default_head.png"),
                                    fit: BoxFit.fill,
                                    image: state.selectImage == null
                                        ? CachedNetworkImageProvider(
                                            NetworkConfig.imageUrl(
                                                state.userInfo.headimg))
                                        : FileImageEx(state.selectImage),
                                    width: 33,
                                    height: 33,
                                  ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(
                              left: 8,
                            ),
                            child: Image.asset('images/right_arrow.png'),
                          ),
                        ],
                      )
                    : Text(
                        _values[i],
                        style: TextThemeConfig.Font14WithColor99,
                      ),
              ],
            ),
            onTap: () => dispatch(UserInfoActionCreator.onTap(i)),
          ),
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(
            color: i == _titles.length - 1
                ? Colors.white
                : ThemeConfig.separeColor,
          ))),
        ),
      );
    }
    return list;
  }

  return Scaffold(
    resizeToAvoidBottomPadding: false,
    appBar: myAppBar(viewService.context, title: '我的资料'),
    body: Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 15),
          color: Colors.white,
          child: Column(
            children: _items(),
          ),
        ),
        Container(
          height: 50,
          margin: EdgeInsets.only(top: 50, left: 15, right: 15),
          decoration: BoxDecoration(
            color: ThemeConfig.mainColor,
            borderRadius: BorderRadius.all(Radius.circular(4)),
          ),
          child: InkWell(
            child: Center(
              child: Text(
                '保存',
                style: TextThemeConfig.Font16WithWhite,
              ),
            ),
            onTap: () => dispatch(UserInfoActionCreator.onSubmit()),
          ),
        ),
      ],
    ),
  );
}
